$(function () {

    var type = "0";

    var firstClickGroup = true;

    $(".check_all").click(function () {
        var id = this.id;
        if ($('#' + id).is(":checked")) {
            $('#' + id).prop("checked", true);
            setCheckAll();
        } else {
            $('#' + id).prop("checked", false);
            setUnCheckAll();
        }
    })

    function setCheckAll() {
        $("input[name='check_id']").prop("checked", true);
    }

    function setUnCheckAll() {
        $("input[name='check_id']").prop("checked", false);
    }


    $(".sms_search_group").click(function () {
        var name = $('#group_name').val();
        seachGroup(name)
    })

    function seachGroup(name){
        var url = "/usergroup/admin/query?name=" + name;
        $.ajax({
            url: url,
            dataType: 'json',
            type: 'get',
            success: function (data) {
                loadTableData(data);
            },
            error: function (e) {
                alert("发生错误");
                return false;
            }
        })
    }


    function loadTableData(data) {
        var html;
        var table = $("#group_table");
        for (var i = 0; i < data.length; i++) {
            var d = data[i];
            var tr = "<tr><td>" +
                "<input name='check_id' onclick='changeCheck(this)' id='check" + d.id + "' type='checkbox' value='" + d.id + "'/> " +
                "</td><td>" + d.name + "</td><td>" + d.count + "</td></tr>"
            html += tr;
        }
        table.html(html)
    }


    function checkAll(obj) {
        var id = obj.id;
        if ($('#' + id).is(":checked")) {
            $('#' + id).prop("checked", true);
            setCheckAll();
        } else {
            $('#' + id).prop("checked", false);
            setUnCheckAll();
        }
    }


    $("#choose-by-user").click(function () {
        var userData = $('#userForm').serializeObject();
        var url = "/sms/check/user"
        $.ajax({
            url: url,
            dataType: 'json',
            type: 'get',
            data: "userData=" + userData,
            success: function (data) {
                $('#user-count-label').html("筛选出" + data.data + "人");
            },
            error: function (e) {
                alert("发生错误");
                return false;
            }
        })

    })

    //按钮切换
    $(".type-radio").click(function () {
        var value = this.value;
        if (value == 0) {
            $('#all_user').css('display', 'block');
            $('#user_group').css("display", "none")
            $('#user_custom').css("display", "none")
        } else if (value == 1) {
            $('#user_group').css("display", "block")
            $('#all_user').css("display", "none")
            $('#user_custom').css("display", "none")
            if(firstClickGroup){
                firstClickGroup = false;
                seachGroup("");
            }
        } else {
            $('#all_user').css('display', 'none');
            $('#user_group').css("display", "none")
            $('#user_custom').css("display", "block")
        }
        type = value;
    })

     //发送消息方法
    $("#send-sms-btn").click(function () {
        var groupData = getGroupIds();
        var userdata = $('#userForm').serializeObject();
        var title = $('#msg_title').val();
        var msg = $('#msg').val();

        if(''==title){
            alert('消息主题必填')
            return;
        }
        if(''==msg){
            alert('消息内容必填')
            return;
        }

        var url = "/sms/send/"+type;
        var data = "groupData="+groupData+"&userData="+userdata+"&title="+title+"&msg="+msg;
        $.ajax({
            url: url,
            dataType: 'json',
            type: 'get',
            data: data,
            success: function (data) {
               alert("成功发送"+data.data+"条");
                location.href="/sms/admin/list";
            },
            error: function (e) {
                alert("发生错误");
                return false;
            }
        })


    })


    $.fn.serializeObject = function () {
        var o = {};
        var a = this.serializeArray();
        $.each(a, function () {
            if (o[this.name]) {
                if (!o[this.name].push) {
                    o[this.name] = [o[this.name]];
                }
                o[this.name].push(this.value || '_');
            } else {
                o[this.name] = this.value || '_';
            }
        });
        return JSON.stringify(o);
    };


    function getGroupIds() {
        var ids = "";
        $("input[name='check_id']:checkbox").each(function () {
            if ($(this).is(":checked")) {
                ids += $(this).attr('value') + ',';
            }
        });
        return ids;
    }


    var add = $(".add");
    var del = $(".del");
    var list = $(".list");
    var eee = $(".eee");
    add.click(function () {
        var newS;
        newS = '<div class="list"><span class="del">* </span>' +
            '<select name="userLabel"> <option value="0">姓名</option> <option value="1">手机号码</option> <option value="2">用户来源</option> <option value="3">角色</option> </select>' +
            ' <input type="text" name="userValue" value=""/></div>';
        $(newS).appendTo(".eee");
    });
    eee.delegate(list, "click", function () {
        //  list.remove()
    })

    // $(selector).delegate(childSelector,event,data,function)

});
